package com.hsnn.medstgmini.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.UnrecoverableEntryException;
import java.security.cert.CertificateException;

/**
 * @category 明文密码保存获取
 * @author 蔡春龙
 * @date 2015年6月18日
 */
public class KeystoreUtil {

	/**
	 * @category 保存用户密码
	 * @date 2015年6月18日
	 * @param username
	 * @param password
	 */
	public static void savePassword(String username, String password) {
		/*-FileOutputStream fos = null;
		try {
			KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
			SecretKey SecretKey = new SecretKey
			KeyStore.SecretKeyEntry skEntry = new KeyStore.SecretKeyEntry(
					SecretKey);
			ks.setEntry("secretKeyAlias", skEntry,
					new KeyStore.PasswordProtection(password));

			ks.store(fos, null);
		} catch (KeyStoreException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (CertificateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (UnrecoverableEntryException e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				fos.close();
			}
		}*/
	}

	/**
	 * @category 获取用户密码
	 * @date 2015年6月18日
	 * @param username
	 * @return
	 */
	public static String getPassword(String username) {
		try {
			KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
			FileInputStream fis = new FileInputStream("keyStoreName");
			ks.load(fis, "MeiYouMiMa".toCharArray());
			KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry) ks
					.getEntry(username, null);
			PrivateKey privateKey = pkEntry.getPrivateKey();
		} catch (KeyStoreException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (CertificateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (UnrecoverableEntryException e) {
			e.printStackTrace();
		}
		return null;
	}

}
